package com.jbp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.model.*;
import com.jbp.service.SkillParamService;
import com.jbp.service.SkillService;
import com.jbp.service.SkillUserService;
import com.jbp.utils.PageUtils;
import com.jbp.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * 用户技能(SkillUser)表控制层
 * @author cc
 * @since 2020-11-19 17:06:55
 */
@Api(tags ="APP接口--SkillUser" )
@RestController
@RequestMapping("rest/skillUser")
public class SkillUserController {
    @Autowired
    private SkillUserService skillUserService;

    @Autowired
    private SkillService skillService;

    @Autowired
    private SkillParamService skillParamService;

    @ApiOperation(value="用户技能列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Skill.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @GetMapping("/mySkills")
    public R mySkills(@RequestParam("userId") Integer userId ){
        List<SkillUser> list = skillUserService.mySkills(userId);
        return R.ok(list);
    }

    /** 我的技能 参数列表 */
    @ApiOperation(value="我的技能 参数列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Skill.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @GetMapping("/mySkillParams")
    public R mySkillParams(@RequestParam Integer skillId, @RequestAttribute("userId") Integer userId){
        SkillUser skillUser = skillUserService.getOne(new QueryWrapper<SkillUser>()
                .eq("user_id", userId)
                .eq("skill_id", skillId)
                .eq("status", 3)
        );
        if (skillUser==null) {
            return R.error("暂未拥有该技能");
        }

        Skill skill = skillService.getById(skillId);
        List<SkillParam> list =  skillParamService.editList(skillId, userId);
        skill.setParams(list);
        return R.ok(skill);
    }

    /** 我的技能 参数列表V2版本，支持参数分组 */
    @ApiOperation(value="我的技能 参数列表V2版本，支持参数分组")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Skill.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @GetMapping("/mySkillParamsV2")
    public R mySkillParamsV2(@RequestParam Integer skillId, @RequestAttribute("userId") Integer userId){
        SkillUser skillUser = skillUserService.getOne(new QueryWrapper<SkillUser>()
                .eq("user_id", userId)
                .eq("skill_id", skillId)
                .eq("status", 3)
        );
        if (skillUser==null) {
            return R.error("暂未拥有该技能");
        }

        Skill skill = skillService.getById(skillId);
        List<SkillParamGroup> list =  skillParamService.editListV2(skillId, userId);
        skill.setParamGroups(list);
        return R.ok(skill);
    }


    @ApiOperation(value="修改申请")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Skill.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @PostMapping("/editSkill")
    public R editSkill(@RequestBody SkillApplyReq req, @RequestAttribute("userId") Integer userId){
        Skill skill = skillService.getById(req.getSkillId());
        if(skill==null) return R.error("技能不存在");
        if(skill.getStatus()==0) return R.error("技能已关闭");

        SkillUser skillUser = skillUserService.getOne(
                new QueryWrapper<SkillUser>().eq("user_id", userId)
                        .eq("skill_id", req.getSkillId())
        );

        if (skillUser==null || skillUser.getStatus()!=3) {
            return R.error("暂未拥有该技能");
        }

        skillUserService.editSkill(userId, req);

        return R.ok();
    }

    @ApiOperation(value="筛选参数")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = SkillParam.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @GetMapping("/filterParams")
    public R filterParams(@RequestParam Integer skillId){
        List<SkillParam> list =  skillParamService.filterParams(skillId);
        return R.ok(list);
    }

    @ApiOperation(value="筛选大神 skillId技能， sex性别 man women, sort 排序 priceAsc priceDesc, filterList筛选")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = SkillUser.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @GetMapping("/filterGods")
    public R filterGods(@RequestParam Map<String, Object> params){
        if (params.get("skillId")==null) {
            return R.error("请选择技能");
        }
        PageUtils page = skillUserService.queryPage(params);

        return R.ok(page);
    }

    @ApiOperation(value="热门大神")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = User.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @GetMapping("/hotGods")
    public R hotGods(@RequestParam Map<String, Object> params){
        PageUtils page = skillUserService.hotGods(params);

        return R.ok(page);
    }

    @ApiOperation(value="技能详情")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = SkillUser.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @GetMapping("/info")
    public R info(@RequestParam Integer skillUserId, @RequestAttribute("userId") Integer userId){
        SkillUser skillUser = skillUserService.info(skillUserId, userId);
        return R.ok(skillUser);
    }

}